package com.eascs.web.o2o.scheduler;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;

import com.eascs.app.context.ConfigContext;
import com.eascs.app.dao.ConnectionPoolDataSource;
import com.eascs.app.dao.DataSourceSetting;
import com.eascs.app.dao.JdbcSession;
import com.eascs.app.dao.mongo.MongoSession;
import com.eascs.app.dao.mongo.MongoSetting;
import com.eascs.notify.email.entity.EmailSourceSetting;
import com.eascs.web.DefaultAppConfig;

@Configuration
public class AppConfig extends DefaultAppConfig {
	private static List<String> initSqls = new ArrayList<String>();

	{
		initSqls.add("set names utf8mb4;");
	}

	@Bean
	public EmailSourceSetting emailSourceSetting() {
		EmailSourceSetting emailSourceSetting = new EmailSourceSetting();
		ConfigContext.bindConfigObject(emailSourceSetting, "email.");
		return emailSourceSetting;
	}

	@Bean
	public DataSourceSetting dataSourceSettingDb1() {
		DataSourceSetting dataSourceSetting = new DataSourceSetting();
		ConfigContext.bindConfigObject(dataSourceSetting, "db1.");
		return dataSourceSetting;
	}

	@Bean
	public DataSourceSetting dataSourceSettingDb2() {
		DataSourceSetting dataSourceSetting = new DataSourceSetting();
		ConfigContext.bindConfigObject(dataSourceSetting, "db2.");
		return dataSourceSetting;
	}

	@Bean
	public DataSourceSetting dataSourceSettingDbForCrm() {
		DataSourceSetting dataSourceSetting = new DataSourceSetting();
		ConfigContext.bindConfigObject(dataSourceSetting, "db3.");
		return dataSourceSetting;
	}

	@Bean
	public DataSourceSetting dataSourceSettingDbForService() {
		DataSourceSetting dataSourceSetting = new DataSourceSetting();
		ConfigContext.bindConfigObject(dataSourceSetting, "db4.");
		return dataSourceSetting;
	}

	@Bean
	public DataSourceSetting dataSourceSettingDbForWorkflow() {
		DataSourceSetting dataSourceSetting = new DataSourceSetting();
		ConfigContext.bindConfigObject(dataSourceSetting, "db5.");
		return dataSourceSetting;
	}

	/**
	 * 带修改权限的OA数据库账号
	 * 
	 * @return
	 */
	@Bean
	public DataSourceSetting dataSourceSettingDbForOaWithModify() {
		DataSourceSetting dataSourceSetting = new DataSourceSetting();
		ConfigContext.bindConfigObject(dataSourceSetting, "db6.");
		return dataSourceSetting;
	}

	@Bean
	public DataSourceSetting dataSourceSettingDbForH5() {
		DataSourceSetting dataSourceSetting = new DataSourceSetting();
		ConfigContext.bindConfigObject(dataSourceSetting, "db7.");
		return dataSourceSetting;
	}

	@Bean
	public DataSourceSetting dataSourceSettingDbForSlaveService() {
		DataSourceSetting dataSourceSetting = new DataSourceSetting();
		ConfigContext.bindConfigObject(dataSourceSetting, "db8.");
		return dataSourceSetting;
	}

	@Bean
	public DataSource dataSourceDb1() {
		DataSourceSetting dataSourceSetting = dataSourceSettingDb1();
		ConnectionPoolDataSource connectionPoolDataSource = new ConnectionPoolDataSource();
		connectionPoolDataSource.setDriverClassName(dataSourceSetting.getDriver());
		connectionPoolDataSource.setUrl(dataSourceSetting.getUrl());
		connectionPoolDataSource.setUsername(dataSourceSetting.getUsername());
		connectionPoolDataSource.setPassword(dataSourceSetting.getPassword());
		connectionPoolDataSource.setValidationQuery(dataSourceSetting.getValidationQuery());
		connectionPoolDataSource.setMaxActive(16);
		connectionPoolDataSource.setDatabaseName(dataSourceSetting.getDatabaseName());
		connectionPoolDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
		connectionPoolDataSource.setConnectionInitSqls(initSqls);
		return connectionPoolDataSource;
	}

	@Bean
	public DataSource dataSourceDb2() {
		DataSourceSetting dataSourceSetting = dataSourceSettingDb2();
		ConnectionPoolDataSource connectionPoolDataSource = new ConnectionPoolDataSource();
		connectionPoolDataSource.setDriverClassName(dataSourceSetting.getDriver());
		connectionPoolDataSource.setUrl(dataSourceSetting.getUrl());
		connectionPoolDataSource.setUsername(dataSourceSetting.getUsername());
		connectionPoolDataSource.setPassword(dataSourceSetting.getPassword());
		connectionPoolDataSource.setValidationQuery(dataSourceSetting.getValidationQuery());
		connectionPoolDataSource.setMaxActive(16);
		connectionPoolDataSource.setDatabaseName(dataSourceSetting.getDatabaseName());
		connectionPoolDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
		return connectionPoolDataSource;
	}

	@Bean
	public DataSource dataSourceDbForService() {
		DataSourceSetting dataSourceSetting = dataSourceSettingDbForService();
		ConnectionPoolDataSource connectionPoolDataSource = new ConnectionPoolDataSource();
		connectionPoolDataSource.setDriverClassName(dataSourceSetting.getDriver());
		connectionPoolDataSource.setUrl(dataSourceSetting.getUrl());
		connectionPoolDataSource.setUsername(dataSourceSetting.getUsername());
		connectionPoolDataSource.setPassword(dataSourceSetting.getPassword());
		connectionPoolDataSource.setValidationQuery(dataSourceSetting.getValidationQuery());
		connectionPoolDataSource.setMaxActive(16);
		connectionPoolDataSource.setDatabaseName(dataSourceSetting.getDatabaseName());
		connectionPoolDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
		connectionPoolDataSource.setConnectionInitSqls(initSqls);
		return connectionPoolDataSource;
	}

	@Bean
	public DataSource dataSourceDbForCrm() {
		DataSourceSetting dataSourceSetting = dataSourceSettingDbForCrm();
		ConnectionPoolDataSource connectionPoolDataSource = new ConnectionPoolDataSource();
		connectionPoolDataSource.setDriverClassName(dataSourceSetting.getDriver());
		connectionPoolDataSource.setUrl(dataSourceSetting.getUrl());
		connectionPoolDataSource.setUsername(dataSourceSetting.getUsername());
		connectionPoolDataSource.setPassword(dataSourceSetting.getPassword());
		connectionPoolDataSource.setValidationQuery(dataSourceSetting.getValidationQuery());
		connectionPoolDataSource.setMaxActive(16);
		connectionPoolDataSource.setDatabaseName(dataSourceSetting.getDatabaseName());
		connectionPoolDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
		connectionPoolDataSource.setConnectionInitSqls(initSqls);
		return connectionPoolDataSource;
	}

	@Bean
	public DataSource dataSourceDbForWorkflow() {
		DataSourceSetting dataSourceSetting = dataSourceSettingDbForWorkflow();
		ConnectionPoolDataSource connectionPoolDataSource = new ConnectionPoolDataSource();
		connectionPoolDataSource.setDriverClassName(dataSourceSetting.getDriver());
		connectionPoolDataSource.setUrl(dataSourceSetting.getUrl());
		connectionPoolDataSource.setUsername(dataSourceSetting.getUsername());
		connectionPoolDataSource.setPassword(dataSourceSetting.getPassword());
		connectionPoolDataSource.setValidationQuery(dataSourceSetting.getValidationQuery());
		connectionPoolDataSource.setMaxActive(16);
		connectionPoolDataSource.setDatabaseName(dataSourceSetting.getDatabaseName());
		connectionPoolDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
		connectionPoolDataSource.setConnectionInitSqls(initSqls);
		return connectionPoolDataSource;
	}

	@Bean
	public DataSource dataSourceDbForOaWithModify() {
		DataSourceSetting dataSourceSetting = dataSourceSettingDbForOaWithModify();
		ConnectionPoolDataSource connectionPoolDataSource = new ConnectionPoolDataSource();
		connectionPoolDataSource.setDriverClassName(dataSourceSetting.getDriver());
		connectionPoolDataSource.setUrl(dataSourceSetting.getUrl());
		connectionPoolDataSource.setUsername(dataSourceSetting.getUsername());
		connectionPoolDataSource.setPassword(dataSourceSetting.getPassword());
		connectionPoolDataSource.setValidationQuery(dataSourceSetting.getValidationQuery());
		connectionPoolDataSource.setMaxActive(16);
		connectionPoolDataSource.setDatabaseName(dataSourceSetting.getDatabaseName());
		connectionPoolDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
		return connectionPoolDataSource;
	}

	@Bean
	public DataSource dataSourceDbForH5() {
		DataSourceSetting dataSourceSetting = dataSourceSettingDbForH5();
		ConnectionPoolDataSource connectionPoolDataSource = new ConnectionPoolDataSource();
		connectionPoolDataSource.setDriverClassName(dataSourceSetting.getDriver());
		connectionPoolDataSource.setUrl(dataSourceSetting.getUrl());
		connectionPoolDataSource.setUsername(dataSourceSetting.getUsername());
		connectionPoolDataSource.setPassword(dataSourceSetting.getPassword());
		connectionPoolDataSource.setValidationQuery(dataSourceSetting.getValidationQuery());
		connectionPoolDataSource.setMaxActive(16);
		connectionPoolDataSource.setDatabaseName(dataSourceSetting.getDatabaseName());
		connectionPoolDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
		return connectionPoolDataSource;
	}

	@Bean
	public DataSource dataSourceDbForSlaveService() {
		DataSourceSetting dataSourceSetting = dataSourceSettingDbForSlaveService();
		ConnectionPoolDataSource connectionPoolDataSource = new ConnectionPoolDataSource();
		connectionPoolDataSource.setDriverClassName(dataSourceSetting.getDriver());
		connectionPoolDataSource.setUrl(dataSourceSetting.getUrl());
		connectionPoolDataSource.setUsername(dataSourceSetting.getUsername());
		connectionPoolDataSource.setPassword(dataSourceSetting.getPassword());
		connectionPoolDataSource.setValidationQuery(dataSourceSetting.getValidationQuery());
		connectionPoolDataSource.setMaxActive(16);
		connectionPoolDataSource.setDatabaseName(dataSourceSetting.getDatabaseName());
		connectionPoolDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
		connectionPoolDataSource.setConnectionInitSqls(initSqls);
		return connectionPoolDataSource;
	}

	@Bean(name = "transactionManager")
	public PlatformTransactionManager transactionManagerDb1() {
		DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
		transactionManager.setDataSource(dataSourceDb1());
		return transactionManager;
	}

	@Bean(name = "transactionManagerDb2")
	public PlatformTransactionManager transactionManagerDb2() {
		DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
		transactionManager.setDataSource(dataSourceDb2());
		return transactionManager;
	}

	@Bean(name = "transactionManagerForService")
	public PlatformTransactionManager transactionManagerDbForService() {
		DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
		transactionManager.setDataSource(dataSourceDbForService());
		return transactionManager;
	}

	@Bean(name = "transactionManagerDbForCrm")
	public PlatformTransactionManager transactionManagerDbForCrm() {
		DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
		transactionManager.setDataSource(dataSourceDbForCrm());
		return transactionManager;
	}

	@Bean(name = "transactionManagerDbForWorkflow")
	public PlatformTransactionManager transactionManagerDbForWorkflow() {
		DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
		transactionManager.setDataSource(dataSourceDbForWorkflow());
		return transactionManager;
	}

	@Bean(name = "transactionManagerDbFoOaWithModify")
	public PlatformTransactionManager transactionManagerDbForOaWithModify() {
		DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
		transactionManager.setDataSource(dataSourceDbForOaWithModify());
		return transactionManager;
	}

	@Bean(name = "transactionManagerDbForH5")
	public PlatformTransactionManager transactionManagerDbForH5() {
		DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
		transactionManager.setDataSource(dataSourceDbForH5());
		return transactionManager;
	}

	@Bean(name = "transactionManagerDbForSlaveService")
	public PlatformTransactionManager transactionManagerDbForSlaveService() {
		DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
		transactionManager.setDataSource(dataSourceDbForSlaveService());
		return transactionManager;
	}

	@Bean(name = "jdbcSession")
	public JdbcSession jdbcSessionDb1() {
		JdbcSession jdbcSession = new JdbcSession();
		jdbcSession.setDataSource(this.dataSourceDb1());
		return jdbcSession;
	}

	@Bean(name = "jdbcSessionForOracle")
	public JdbcSession jdbcSessionDb2() {
		JdbcSession jdbcSession = new JdbcSession();
		jdbcSession.setDataSource(this.dataSourceDb2());
		return jdbcSession;
	}

	@Bean(name = "jdbcSessionForService")
	public JdbcSession jdbcSessionDbForService() {
		JdbcSession jdbcSession = new JdbcSession();
		jdbcSession.setDataSource(this.dataSourceDbForService());
		return jdbcSession;
	}

	@Bean(name = "jdbcSessionForCrm")
	public JdbcSession jdbcSessionDbForCrm() {
		JdbcSession jdbcSession = new JdbcSession();
		jdbcSession.setDataSource(this.dataSourceDbForCrm());
		return jdbcSession;
	}

	@Bean(name = "jdbcSessionForWorkflow")
	public JdbcSession jdbcSessionDbForWorkflow() {
		JdbcSession jdbcSession = new JdbcSession();
		jdbcSession.setDataSource(this.dataSourceDbForWorkflow());
		return jdbcSession;
	}

	@Bean(name = "jdbcSessionForOaWithModify")
	public JdbcSession jdbcSessionDbForOaWithModify() {
		JdbcSession jdbcSession = new JdbcSession();
		jdbcSession.setDataSource(this.dataSourceDbForOaWithModify());
		return jdbcSession;
	}

	@Bean(name = "jdbcSessionForH5")
	public JdbcSession jdbcSessionForH5() {
		JdbcSession jdbcSession = new JdbcSession();
		jdbcSession.setDataSource(this.dataSourceDbForH5());
		return jdbcSession;
	}

	@Bean(name = "jdbcSessionForSlaveService")
	public JdbcSession jdbcSessionForSlaveService() {
		JdbcSession jdbcSession = new JdbcSession();
		jdbcSession.setDataSource(this.dataSourceDbForSlaveService());
		return jdbcSession;
	}

	@Bean(name = "SchedulerFactoryBean")
	public SchedulerFactoryBean schedulerFactoryBean() {
		return new SchedulerFactoryBean();
	}

	/*
	 * @Bean public DubboConsumerInterceptor dubboConsumerInterceptor() { return
	 * new DubboConsumerInterceptor(); }
	 */
	@Bean
	public static PropertySourcesPlaceholderConfigurer placehodlerConfigurer() {
		return new PropertySourcesPlaceholderConfigurer();
	}

	@Bean(name = "mongoSessionS")
	public MongoSession mongoSession() {
		MongoSetting mongoSetting = new MongoSetting();
		ConfigContext.bindConfigObject(mongoSetting);
		return new MongoSession(mongoSetting);
	}
}
